﻿//TODO: XOR, Test

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// XORs two images
    /// </summary>
    public class XOR : BaseImageFilter, ITwoSourceFilter
    {
        public XOR()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="XOR"/> class.
        /// </summary>
        /// <param name="_xor">The _xor.</param>
        internal XOR(XOR _xor)
        {
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            Process((UnsafeBitmap)_bitmap.Clone(), _bitmap);
            return _bitmap;
        }

        /// <summary>
        /// Processes the specified bmpimg.
        /// </summary>
        /// <param name="_bitmap1"></param>
        /// <param name="_bitmap2"></param>
        /// <returns></returns>
        public UnsafeBitmap Process(UnsafeBitmap _bitmap1, UnsafeBitmap _bitmap2)
        {
            int y = 0;
            int x = 0;
            int nWidth = _bitmap1.Width;
            int nHeight = _bitmap1.Height;

            UnsafeBitmap bitmap = new Bitmap(nWidth, nHeight);

            Color color1 = Color.Transparent;
            Color color2 = Color.Transparent;
            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color1 = _bitmap1.GetPixel(x, y);
                    color2 = _bitmap2.GetPixel(x, y);
                    bitmap.SetPixel(x, y, Color.FromArgb(XOR_Operator(color1.R, color2.R), XOR_Operator(color1.G, color2.G), XOR_Operator(color1.B, color2.B)));
                }
            }

            return bitmap;
        }

        /// <summary>
        /// Processes the specified bmpimg.
        /// </summary>
        /// <param name="_bitmap1"></param>
        /// <param name="_bitmap2"></param>
        /// <returns></returns>
        public UnsafeBitmap Process(UnsafeBitmap _bitmap1, int _nConstant)
        {
            int y = 0;
            int x = 0;
            int nWidth = _bitmap1.Width;
            int nHeight = _bitmap1.Height;

            UnsafeBitmap bitmap = new Bitmap(nWidth, nHeight);

            Color color1 = Color.Transparent;
            Color color2 = Color.Transparent;
            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color1 = _bitmap1.GetPixel(x, y);
                    bitmap.SetPixel(x, y, Color.FromArgb(color1.ToArgb() ^ _nConstant));
                }
            }

            return bitmap;
        }

        /// <summary>
        /// Processes the specified bmpimg.
        /// </summary>
        /// <param name="_bitmap1"></param>
        /// <param name="_bitmap2"></param>
        /// <returns></returns>
        public UnsafeBitmap Process(UnsafeBitmap _bitmap1, byte _bConstant)
        {
            int y = 0;
            int x = 0;
            int nWidth = _bitmap1.Width;
            int nHeight = _bitmap1.Height;

            UnsafeBitmap bitmap = new Bitmap(nWidth, nHeight);

            Color color1 = Color.Transparent;
            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color1 = _bitmap1.GetPixel(x, y);
                    bitmap.SetPixel(x, y, Color.FromArgb(XOR_Operator(color1.R, _bConstant), XOR_Operator(color1.G, _bConstant), XOR_Operator(color1.B, _bConstant)));
                }
            }
            return bitmap;
        }

        /// <summary>
        /// XOs the r_ operator.
        /// </summary>
        /// <param name="a">A.</param>
        /// <param name="b">The b.</param>
        /// <returns></returns>
        private byte XOR_Operator(byte a, byte b)
        {
            byte A = (byte)(255 - a);
            byte B = (byte)(255 - b);
            return (byte)((a & B) | (A & b));
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new XOR(this);
        }
    }
}
